<?php
declare (strict_types=1);

namespace app\common\model;


class Order extends Base
{
    protected $name = 'order';

    protected $type = [
        'post_time'    => 'timestamp',
        'pay_time'     => 'timestamp',
        'receipt_time' => 'timestamp',
    ];

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,photo,mobile,level_id,tag');
    }

    public function express()
    {
        return $this->hasOne(Express::class, 'id', 'express_id')->field('id,title');
    }

    public function p()
    {
        return $this->hasOne(Area::class, 'id', 'province')->field('id,ext_name as title');
    }

    public function c()
    {
        return $this->hasOne(Area::class, 'id', 'city')->field('id,ext_name as title');
    }

    public function d()
    {
        return $this->hasOne(Area::class, 'id', 'district')->field('id,ext_name as title');
    }

    public function productsField()
    {
        return $this->hasMany(OrderProduct::class, 'order_id')
            ->field('id,order_id,pid,title,num,spec,vip_price,icon');
    }

    public function getPayWayAttr($value)
    {
        return get_pay_way($value);
    }

    public function getStatusTextAttr($value, $data)
    {
        return self::getOrderStatus($data['status']);
    }

    public static function getOrderStatus($value, $type = 1)
    {
        $status = [1 => '待付款', 2 => '待发货', 3 => '待收货', 4 => '已完成', 7 => '已取消'];
        return $type == 1 ? $status[$value] : $status;
    }
    /**
     * 收货地址
     */
    public function getAddressTextAttr($value,$data)
    {
        return ;
    }
    

    /**
     * @param $param -- 提交的参数
     * @param $admin -- 后台当前登录管理员
     * @return array
     * 原点 2022年1月8日16:45:49 后台发货订单
     */
    public static function deliver($param, $admin)
    {

        try {
            $info = parent::where('id', $param['id'])->find();
            if ($info->isEmpty()) {
                return ['code' => -1, 'msg' => '未查询到相关订单'];
            }
            $status = $info->status;
            $info->express_id = $param['express_id'];
            $info->express_code = $param['express_code'];
            $info->receiver = $param['receiver'];
            $info->receiver_tel = $param['receiver_tel'];
            $info->province = $param['province'];
            $info->city = $param['city'];
            $info->district = $param['district'];
            $info->address_details = $param['address_details'];
            $info->post_remark = $param['post_remark'];
            $info->post_time = time();

            if ($status == 2) {
                $info->status = 3;
            }
            $res = $info->save();
            if ($res !== true) {
                return ['code' => -1, 'msg' => '发货失败'];
            }

            if ($status == 2) {
                $msg = '订单发货，订单号【' . $info->sn . '】下单会员【' . $info->user->code . '】';
                $m = '已发货';
            } else {
                $msg = '修改了发货信息，订单号【' . $info->sn . '】下单会员【' . $info->user->code . '】';
                $m = '已修改';
            }
            Logs::adds($msg, $admin->id, $admin->admin_name);

            return ['code' => 1, 'msg' => $m];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
